<?php
namespace app\admin\controller;

use app\common\controller\Admin;

class User extends Admin
{
    //定义表单字段与属性
    protected function fields($action='create', $data=[])
    {
        //合并初始数据
        $data = DcArrayArgs(config('form_data.admin_user_'.$action), $data);
        //所有分类字段预埋钩子
        return DcArrayArgs(config('form_fields.admin_user_'.$action), \daicuo\User::fields($data));
    }
    
    //添加(数据库)
	public function save()
    {
        config('common.validate_name', config('validate_name.admin_user_save'));
        
        config('common.validate_scene', config('validate_scene.admin_user_save'));
        
        config('common.where_slug_unique', config('slug_unique.admin_user_save'));
        
        $user_id = \daicuo\User::save(input('post.'));
		if($user_id < 1){
			$this->error(\daicuo\User::getError());
		}
		$this->success(lang('success'));
	}
    
	//删除(数据库)
	public function delete()
    {
		$ids = input('id/a');
		if(!$ids){
			$this->error(lang('mustIn'));
		}
        
        foreach($ids as $user_id){
            \daicuo\User::delete_user_by('user_id', $user_id);
        }
        
        $this->success(lang('success'));
	}
    
	//修改（表单）
	public function edit()
    {
		if( !$user_id = input('id/d',0) ){
			$this->error(lang('mustIn'));
		}
		//查询数据
        $data = \daicuo\User::get_user_by('user_id', $user_id, false);
        if( is_null($data) ){
            $this->error(\daicuo\User::getError());
        }
        //删除密码
        unset($data['user_pass']);
        //加载模板
        $this->assign('query', $this->query);
		$this->assign('data', $data);
        $this->assign('fields', $this->formFields('edit', $data));
		return $this->fetch();
	}
	
	//修改（数据库）
	public function update()
    {  
		$data = input('post.');
        if(!$data['user_id']){
            $this->error(lang('mustIn'));
        }
        
        config('common.validate_name', config('validate_name.admin_user_update'));
        
        config('common.validate_scene', config('validate_scene.admin_user_update'));
        
        config('common.where_slug_unique', config('slug_unique.admin_user_update'));
        
        $info = \daicuo\User::update_user_by('user_id', $data['user_id'], $data);
        if(is_null($info)){
            $this->error(\daicuo\User::getError());
        }
        $this->success(lang('success'));
	}
    
    // 数据查询 ?pageNumber=1&pageSize=20&sortName=user_id&sortOrder=desc&queryKey=user_score&queryValue=2&queryRelation=gt
	public function index()
    {
        if($this->request->isAjax()){
            $args = array();
            $args['cache']         = false;
            $args['field']         = '*';
            $args['limit']         = input('pageSize/d', 20);
            $args['page']          = input('pageNumber/d', 1);
            $args['sort']          = input('get.sortName/s','user_id');
            $args['order']         = input('get.sortOrder/s','desc');
            $args['search']        = input('searchText/s','');
            $args['queryKey']      = input('queryKey/s','');
            $args['queryValue']    = input('queryValue/s','');
            $args['queryRelation'] = trim(input('queryRelation/s','eq'));
            //初始字段条件
            $args['where']         = DcWhereFilter($this->query, ['user_module','user_controll','user_action','user_token','user_status','user_create_ip','user_update_ip'], 'eq');
            if(!$args['where']){
                unset($args['where']);
            }
            //用户组
            if( input('get.user_capabilities/s') ){
                $args['queryKey']      = 'user_capabilities';
                $args['queryValue']    = '%'.input('get.user_capabilities/s').'%';
                $args['queryRelation'] = 'like';
            }
            //自定义字段
            if($args['queryKey'] && $args['queryValue']){
                $args['meta_query'] = [
                    [
                        'key'   => ['eq', $args['queryKey']],
                        'value' => [$args['queryRelation'], $args['queryValue']],
                    ],
                ];
            }
            //查询数扰2021.08.07
            $list = DcUserSelect($args);
            if(!is_null($list)){
                return json($list);
            }else{
                return json(['total'=>0,'data'=>'']);
            }
            //$args['fetchSql'] = true;
            //$args['where'] = [];
            //$args['whereOr'] = ['user_name'=>['like','%tan%'],'user_email'=>['like','%adm%']];
            /*if($search = input('searchText/s')){
                $args['whereOr'] = [
                    'user_name'=>['like','%'.$search.'%'],
                    'user_email'=>['like','%'.$search.'%'],
                    'user_mobile'=>['like','%'.$search.'%'],
                ];
            }*/
		}
        //地址栏参数
        $this->assign('query', $this->query);
        //表单筛选字段
        $this->assign('fields', $this->formFields('index', $this->query));
        //表格列字段
        $this->assign('columns', $this->tableColumns('index', $this->query));
        //加载模板
		return $this->fetch();
	}
}